/*
 * @Author: 刘润泽
 * @Date: 2022-11-09 19:07:43
 * @LastEditors: 刘润泽
 * @LastEditTime: 2023-03-11 17:10:37
 * @Description: 取血记录
 */
package server.business.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import server.business.pojo.BloodTake;
import server.business.service.AutologousBloodInformationService;
import server.business.service.BloodInformationService;
import server.business.service.BloodTakeService;
import server.business.utils.GenerateNumberBySnowFlake;
import server.business.utils.results.lrz.Result;

import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @author simba@onlying.cn
 * @date 2022/10/1 12:49
 */
@RestController
@RequestMapping("/bloodTake")
public class BloodTakeController {
    @Autowired
    BloodTakeService bloodTakeService;

    @Autowired
    AutologousBloodInformationService autologousBloodInformationService;

    @Autowired
    BloodInformationService bloodInformationService;

    /**
     * 分页查询
     *
     * @param currentPage
     * @param pageSize
     * @param bloodTake
     * @return
     */
    @GetMapping("/normal/{currentPage}/{pageSize}")
    public Result getPage(@PathVariable int currentPage, @PathVariable int pageSize,
            BloodTake bloodTake) {
        IPage<BloodTake> page = bloodTakeService.getPage(currentPage, pageSize, bloodTake);
        // 如果当前页码值大于了总页码值，就重新执行查询操作，使用最大页码值作为当前页码值
        if (currentPage > page.getPages()) {
            page = bloodTakeService.getPage((int) page.getPages(), pageSize, bloodTake);
        }
        return Result.success(page);
    }

    /**
     * 检测取血表中是否已经有了记录
     * 
     * @param transfusionSurgey
     * @return
     */
    @GetMapping(value = "/normal/checkRecords")
    public Result checkRecords(BloodTake bloodTake) {
        List<BloodTake> bloodTakes = bloodTakeService.checkRecords(bloodTake);
        return Result.success(bloodTakes.size());
    }

    /**
     * 血液发血后添加一条待取血记录
     * 
     * @param bloodTake
     * @return
     */
    @RequestMapping(value = "/normal/addRecord", method = RequestMethod.POST)
    public Result addRecord(@RequestBody BloodTake bloodTake) {
        bloodTakeService.addRecord(bloodTake);
        return Result.success("待取血记录添加成功");
    }

    /**
     * 前来取血补足表格内其他信息
     * 
     * @param bloodTake
     * @return
     */
    @RequestMapping(value = "/normal/editData", method = RequestMethod.PUT)
    @ResponseBody
    public Result editData(@RequestBody BloodTake bloodTake) {
        GenerateNumberBySnowFlake bloodTakeNumber = new GenerateNumberBySnowFlake(
                Integer.parseInt(bloodTake.getBloodTakerJobnum()), "BT");
        bloodTake.setBloodTakeNumber(bloodTakeNumber.genNumberByflag());
        bloodTakeService.editData(bloodTake);
        return Result.success("信息登记成功");
    }

    /**
     * 批量删除
     * 
     * @param ids
     * @return
     */
    @RequestMapping("/normal/batchDelete")
    @ResponseBody
    public Result batchDelete(@RequestBody ArrayList<Long> ids) {
        System.out.println(ids);
        boolean success = bloodTakeService.removeByIds(ids);
        Result result = new Result();
        if (success) {
            result.setCode("200");
            result.setMsg("删除成功");
        } else {
            result.setCode("500");
            result.setMsg("删除失败");
        }
        return result;
    }

}
